package com.a16.yuejian.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.javabean.Collected;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.CollectedListener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.model.GameModel;

public class GmDetailActivity extends AppCompatActivity implements View.OnClickListener{
    private WebView wv;
    private ImageView back,follow;
    private Intent intent;
    private SharedPreferences sp;

    SuccessListener followListener =new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(GmDetailActivity.this,"关注成功", Toast.LENGTH_SHORT).show();
                follow.setImageResource(R.drawable.ic_aixin1);
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(GmDetailActivity.this,"成功取消关注", Toast.LENGTH_SHORT).show();
                follow.setImageResource(R.drawable.ic_aixin);
            }
        }

        @Override
        public void onFail() {

        }
    };

    CollectedListener collectedListener = new CollectedListener() {
        @Override
        public void onResponse(Collected collected) {
            if(collected.getCollected().equals("1")){
                follow.setImageResource(R.drawable.ic_aixin1);
            }else if(collected.getCollected().equals("0")){
                follow.setImageResource(R.drawable.ic_aixin);
            }
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gm_detail);
        initViews();
        initEvent();
        GameModel gameModel = new GameModel();
        gameModel.isCollected(getIntent().getStringExtra("gid"),sp.getString("uid",""),"2",collectedListener);
        wv.loadUrl(getIntent().getStringExtra("url"));
        wv.setWebViewClient(new WebViewClient());
    }

    private void initEvent() {
        back.setOnClickListener(this);
        follow.setOnClickListener(this);
    }

    private void initViews() {
        wv = (WebView)findViewById(R.id.webView);
        back = (ImageView)findViewById(R.id.ga_detail_back);
        follow = (ImageView)findViewById(R.id.ga_detail_follow);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        wv.getSettings().setJavaScriptEnabled(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ga_detail_back:
                finish();
                break;

            case R.id.ga_detail_follow:
                GameModel gameModel = new GameModel();
                gameModel.follow(getIntent().getStringExtra("gid"),sp.getString("uid",""),"2",followListener);
                break;
        }
    }
}
